package org.compiere.report;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.compiere.model.MAttachmentEntry;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.SvrProcess;
import org.compiere.util.Env;
import org.compiere.util.Msg;

import extend.org.compiere.utils.Util;

/**
 * @author A.Kulantayev
 *
 */
public class A_Asset_Form_326_IN extends SvrProcess {
	//Form_326_IN_
			/** Current context */
			private Properties m_ctx;
			private ProcessInfo pi;
			int p_A_Asset_Transaction_ID;
			/** */
			String file;
			/**
			 * Prepare - e.g., get Parameters.
			 */
			@Override
			protected void prepare() {
				log.info("");
				m_ctx = Env.getCtx();
				pi = getProcessInfo();
				ProcessInfoParameter[] para = getParameter();
				for (int i = 0; i < para.length; i++) {
					String name = para[i].getParameterName();
					if (para[i].getParameter() == null)
						;
					else if (name.equals("A_Asset_Transaction_ID")) 
						p_A_Asset_Transaction_ID  = para[i].getParameterAsInt();
					else
						log.log(Level.SEVERE, "Unknown Parameter: " + name);
				}
			}
			@Override
			protected String doIt() throws Exception {
				file = Util.localFilePath + "Form_326_IN_.xls";
				MAttachmentEntry entry = Util.getAttachment(pi, m_ctx, file);
				if (entry == null)
					throw new AdempiereException(Msg.translate(m_ctx,"NotFoundTemplate"));

				HSSFWorkbook wb = new HSSFWorkbook(entry.getInputStream());
				HSSFSheet sheet = wb.getSheetAt(0);
				Row R = null;
				Cell C = null;
					// Write the output to a file
				FileOutputStream fileOut = new FileOutputStream(file);
				wb.write(fileOut);
				fileOut.close();
				// Open the file with the application associated
							Util.launchFile(new File(file));	
				return Msg.translate(m_ctx, "Success");
			}
}
